package games.tankwars.behaviors;

import games.tankwars.Level;
import games.tankwars.entities.Bullet;
import games.tankwars.entities.Gun;
import games.tankwars.entities.MazeElement;
import games.tankwars.entities.Tank;
import vooga.behaviors.MoveBehavior;
import vooga.core.AbstractLevel;
import vooga.entities.Entity;
import vooga.geom.Vector;

public class StaionaryGun extends vooga.behaviors.AbstractBehavior {
	
	private int BULLET_MOVE_SPEED=5;
	private AbstractLevel myLevel;
	private Tank myTank;
	private int myTimer;
	private boolean noObstruction;
	public StaionaryGun(Tank tank, AbstractLevel level) {
		myLevel = level;
		myTank = tank;
		myTimer=0;
		noObstruction=true;
	}
	@Override
	public void step(Entity target) {
		Vector Velocity = new Vector (); 
		// TODO Auto-generated method stub
		myTimer++;
		double dx=myTank.getCenter().getX()-target.getCenter().getX();
		double dy=myTank.getCenter().getY()-target.getCenter().getY();
		double dDistance=Math.sqrt(dx*dx+dy*dy);	
		
		int heading= (int) Math.toDegrees(Math.atan(dy/dx));
		if (dx<0)
			heading+=180;
		target.setHeading(heading);
		
//		System.out.println(Math.toDegrees(Math.atan(dy/dx)));

		
//		System.out.println(myTimer+"-"+myLevel.getFrameRate()*5);
//		System.out.println(myTimer%myLevel.getFrameRate()*5);
		if (myTimer%myLevel.getFrameRate()*5 == 0)
		{
			if (dDistance<300 && myTank.isVisible() && noObstruction)
			{
				System.out.print("new bullet");
				Bullet newBullet = new games.tankwars.entities.Bullet((java.awt.geom.Point2D.Double)(target.getCenter().clone()));
				newBullet.setVelocity(new Vector(target.getHeading(),BULLET_MOVE_SPEED));
				newBullet.addBehavior(new MoveBehavior());
				newBullet.addBehavior (new IntersectAndRemoveBoth(Velocity, myTank,myLevel));
				newBullet.addBehavior (new IntersectAndRemove(Velocity, myLevel.getEntities("maze"),myLevel));
				myLevel.addEntity(newBullet);
			}
		}
		
	}

}
